package org.wz.complex.work.except;

import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.wz.complex.work.bean.Res;



@ControllerAdvice
public class GlobalExceptionHandler {

    // 处理自定义异常
    @ExceptionHandler(DuplicateKeyException.class)
    public ResponseEntity<Res> sQLIntegrityConstraintViolationException(DuplicateKeyException ex) {
        System.out.println(ex.getMessage());
        ex.printStackTrace();
        // HttpStatus.TOO_MANY_REQUESTS
        return new ResponseEntity<>(Res.fail("数据库字段唯一性校验失败！"), HttpStatus.TOO_MANY_REQUESTS);
    }

    // 处理其他未捕获的异常
    @ExceptionHandler(Exception.class)
    public ResponseEntity<Res> handleGeneralException(Exception ex) {
        System.out.println(ex.getMessage());
        ex.printStackTrace();
        return new ResponseEntity<>(Res.fail(ex.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}